<template>
  <el-dialog v-model="deleteVisible" :title="title" width="50%" align-center class="public-dialog" @close="cancel"
    :close-on-click-modal="false">
    <div class="content">
      <div class="name">
        <span>英文名: {{ ruleForm.userinfo.en_name }}</span> <span>真实姓名: {{ ruleForm.userinfo.realname }}</span>
      </div>
      <el-tabs type="border-card" class="tab-pane">
        <el-tab-pane label="菜单权限">
          <el-cascader-panel v-model="ruleForm.menu_ids" ref="dataMenu" :options="menuOptions" :props="cascaderProps"
            class="cascaderPanel" />
        </el-tab-pane>
        <el-tab-pane label="按钮授权">
          <el-cascader-panel v-model="ruleForm.button_ids" ref="dataButton" :options="buttonOptions"
            :props="cascaderProps" class="cascaderPanel" />
        </el-tab-pane>
        <el-tab-pane label="数据权限">
          <el-cascader-panel ref="dataUser" :model-value="ruleForm.data_user_ids" :options="options"
            :props="cascaderProps" class="cascaderPanel" />
        </el-tab-pane>
        <el-tab-pane label="属性权限">
          <div class="attribute">
            <el-tree :props="goodsProps" :data="goodsData" show-checkbox
              :default-checked-keys="ruleForm.goods_category_ids" :default-expanded-keys="ruleForm.goods_category_ids"
              ref="dataGoods" node-key="id" />
            <el-tree :props="warehouseProps" :data="warehouseData" show-checkbox :default-checked-keys="warehouse_ids"
              :default-expanded-keys="ruleForm.warehouse_ids" node-key="id" ref="dataWarehouse" />
            <el-tree :props="menuProps" :data="fieldData" show-checkbox :default-checked-keys="ruleForm.field_attr_ids"
              :default-expanded-keys="ruleForm.field_attr_ids" ref="dataField" node-key="id" />
          </div>
        </el-tab-pane>
      </el-tabs>
      <p class="prompt">提示：只能修改账号自定义权限，不能调整账号角色的权限</p>
    </div>
    <template #footer>
      <div class="dialog-button">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="confirm" :loading="loading.save">
          保存
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup name="VRole">
import { organizationList } from "@/api/system/role"
import { getMenus } from "@/api/menu"
import api from '../api'
import { reactive, ref, toRefs, watch } from 'vue'
import { ElMessage } from 'element-plus'
const dataField = ref()
const dataWarehouse = ref()
const dataGoods = ref()
const dataButton = ref()
const dataMenu = ref()
const dataUser = ref()
const emits = defineEmits(['cancel', 'confirm'])
// 声明接收参数
const props = defineProps({
  title: {
    type: String,
    default: "提示"
  },
  formData: {
    type: Object,
    default: {}
  }
})

//递归调用
const treeToArray = tree => {
  const expanded = datas => {
    if (datas && datas.length > 0) {
      datas.forEach(e => {
        e.id = '1-' + e.warehouse_id
        e.label = e.attribute_name ? e.attribute_name : e.warehouse_name
        expanded(e.warehouse)
      })
    }
  }
  expanded(tree)
}
//树型数据转为一维数组
const oneArray = tree => {
  const arr = []
  const expanded = datas => {
    if (datas && datas.length > 0) {
      datas.forEach(e => {
        if (e.son) {
          arr.push(e)
        }
        expanded(e.son)
      })
    }
  }
  expanded(tree)
  return arr
}
const goodsProps = {

  label: 'name',
  children: 'son',
}
const warehouseProps = {
  label: 'label',
  children: 'warehouse',
}
const menuProps = {
  label: 'name',
  children: 'sub',
}
const data = reactive({
  deleteVisible: false,
  ruleForm: {
    userinfo: {
    }
  },
  loading: {},
  cascaderProps: {
    emitPath: false,
    multiple: true,
    label: 'name',
    value: 'id',
    children: 'sub',
  },
  options: [],
  menuOptions: [],
  buttonOptions: [],
  goodsData: [],
  warehouseData: [],
  fieldData: [],
  warehouse_ids: [],
  oneGoods: []
})
const { deleteVisible, ruleForm, loading, cascaderProps, options, buttonOptions, menuOptions, goodsData, warehouseData, fieldData, warehouse_ids, oneGoods } = toRefs(data)
const { formData } = toRefs(props)
watch(() => formData.value, (newVal, oldVal) => {
  ruleForm.value = JSON.parse(JSON.stringify(newVal))
  ruleForm.value.warehouse_ids.map(res => {
    warehouse_ids.value.push('1-' + res)
  })
})
const open = async () => {
  deleteVisible.value = true
  await organizationList().then(res => {
    options.value = res.data
  })
  await api.baseData({ get: ['access'] }).then(res => {
    menuOptions.value = res.data.access
      .menu_options
    buttonOptions.value = res.data.access
      .button_options
    goodsData.value = res.data.access
      .goods_category_options
    warehouseData.value = res.data.access
      .warehouse_options
    treeToArray(warehouseData.value)
    fieldData.value = res.data.access
      .field_attr_options
    oneGoods.value = oneArray(res.data.access
      .goods_category_options)
    let allArr = []
    allArr = ruleForm.value.goods_category_ids.filter(e => oneGoods.value.some(a => a.son.length && a.id === e))
    ruleForm.value.goods_category_ids = ruleForm.value.goods_category_ids.filter(e => !allArr.includes(e))
    dataGoods?.value?.setCheckedKeys(ruleForm.value.goods_category_ids, true)
  })

}
const confirm = () => {
  loading.value.save = true
  const { menu_ids, button_ids, data_user_ids, field_attr_ids, goods_category_ids, warehouse_ids } = ruleForm.value
  api.setAccess({
    id: ruleForm.value.userinfo.user_id, perm: { user_owner: { menu_ids, button_ids, data_user_ids, field_attr_ids, goods_category_ids, warehouse_ids } }
  }).then(res => {
    ElMessage({
      message: '保存成功！',
      type: 'success',
    })
    deleteVisible.value = false
    loading.value.save = false
    emits('confirm')
  }).catch(err => {
    loading.value.save = false
  })
}
const cancel = () => {
  ruleForm.value = {
    userinfo: {
    }
  }
  deleteVisible.value = false
  loading.value.save = false
  emits('cancel')
}
defineExpose({
  open, data, cancel
})
</script>
<style lang="scss" scoped>
.attribute {
  display: flex;
  height: 400px;
  overflow: auto;

  &:deep(.el-tree) {
    flex: 1;
  }
}

.content {
  align-items: center;

  .name {
    margin: 15px;

    span {
      margin-right: 20px;
    }
  }

  .role {
    margin: 0 15px;
    padding-bottom: 10px;
    border-bottom: 1px solid #f7f9fe;
  }

  .radio {
    display: flex;
    margin: 0 15px;
    justify-content: space-between;
  }

  span {
    margin-left: 5px;
  }

  .prompt {
    color: red;
    margin-left: 15px;
  }
}

.dialogName {
  font-size: 16px;
  font-weight: bold;
}

.input-switch {
  width: calc(100% - 58px);
  margin-right: 10px;
}

:deep(.tab-pane) {
  margin: 10px 20px;
  border-radius: 10px;

  .el-tabs__header {
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
  }

  .el-tabs__nav {
    width: 100%;
    border-radius: 10px;
  }

  .el-tabs__item.is-active {
    color: #333;
    background-color: #fafafa;
  }

  .el-tabs__item {
    margin-left: 0 !important;
    width: 25% !important;
    color: #fff;
    text-align: center;
    border: none;
    border-right: 1px solid #fff;
    background: #6d7282;

    &:last-child {
      border-right: none;
    }
  }

  .el-tabs__item:first-child {
    border-top-left-radius: 10px;
  }

  .el-tabs__item:last-child {
    text-align: center;
    border-top-right-radius: 10px;
  }
}

.cascaderPanel {
  overflow: auto;
  height: 400px;

  &:deep(.el-scrollbar__wrap) {
    height: 100% !important;
  }
}
</style>